package com.yiren.dbaa.module.practice_set.site.bean;

import android.content.Context;

import com.google.gson.Gson;
import com.yiren.backstage.currency.constant.SignalGConstant;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.db.dao.bean.SynExamBean;
import com.yiren.backstage.db.dao.cache.DataCache;
import com.yiren.backstage.db.dao.cache.interf.DataCacheStateCallBack;
import com.yiren.dbaa.constant.ExamConfigConstant;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.util.GsonDoubleInteger;

import java.util.Iterator;
import java.util.Map;

/**
 * @author xiaozan
 * @Date : 2022/10/21 下午1:50
 * @description:数据合并
 */
public class ExamMerge {

    private Context context;

    private Gson gson;

    public ExamMerge(Context context) {
        this.context = context;
        gson = GsonDoubleInteger.getGson();
    }

    public void loadExamData(SiteEntity site, CommonCallBack callBack) {
        //加载数据缓存
        DataCache.refreshAllCache(context, site, new DataCacheStateCallBack.Callback() {
            @Override
            public void success() {
                SynExamBean partneSynExamBean = gson.fromJson(site.getPartneModifyVal(), SynExamBean.class);
                SynExamBean coachSynExamBean = gson.fromJson(site.getCoachModifyVal(), SynExamBean.class);
                if (null == partneSynExamBean) partneSynExamBean = new SynExamBean();
                if (null == coachSynExamBean) coachSynExamBean = new SynExamBean();
                configMerge(partneSynExamBean, coachSynExamBean);
                callBack.success("");
            }
        });
    }

    /**
     * 考场配置数据合并
     *
     * @param partneSynExamBean
     * @param coachSynExamBean
     */
    private void configMerge(SynExamBean partneSynExamBean, SynExamBean coachSynExamBean) {
        //参数配置
        if (null == partneSynExamBean.getExamConfigMap() && null == coachSynExamBean.getExamConfigMap()) {
            //取考场配置默认值
            SignalGConstant.examConfigMap = ExamConfigConstant.getExamConfigMap();
        } else {
            //合伙人和教练数据都不为空
            if (null != partneSynExamBean.getExamConfigMap() && null != coachSynExamBean.getExamConfigMap()) {
                //合伙人与教练配置合并
                Map<String, Object> pAndCMergeMap = configMerge(partneSynExamBean.getExamConfigMap(), coachSynExamBean.getExamConfigMap());
                //合并数据与默认数据合并
                SignalGConstant.examConfigMap = configMerge(ExamConfigConstant.getExamConfigMap(), pAndCMergeMap);
            } else {
                //教练数据为空
                if (null == coachSynExamBean.getExamConfigMap()) {
                    //合伙人与默认数据合并
                    SignalGConstant.examConfigMap = configMerge(ExamConfigConstant.getExamConfigMap(), partneSynExamBean.getExamConfigMap());
                } else {
                    //合伙人数据为空 教练与默认数据合并
                    SignalGConstant.examConfigMap = configMerge(ExamConfigConstant.getExamConfigMap(), coachSynExamBean.getExamConfigMap());
                }
            }
        }
    }

    /**
     * 合伙人、教练 考场配置数据合并
     *
     * @param secondaryMap 为辅数据
     * @param mainMap      为主数据 覆盖至为辅数据上
     */
    private Map<String, Object> configMerge(Map<String, Object> secondaryMap, Map<String, Object> mainMap) {
        //遍历教练修改配置
        Iterator<Map.Entry<String, Object>> iterator = mainMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Object> entry = iterator.next();
            secondaryMap.put(entry.getKey(), entry.getValue());
        }
        return secondaryMap;
    }
}
